//
//  GifCollectionViewController.m
//  GIFShow
//
//  Created by lanou11air029 on 15/5/1.
//  Copyright (c) 2015年 CK. All rights reserved.
//

#import "GifCollectionViewController.h"
#import "GifDownload.h"
#import "AppDelegate.h"
#import "SaveFile.h"
#import "SubViewController.h"
#import "GifCollectionViewCell.h"
@interface GifCollectionViewController ()<UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) NSManagedObjectContext * managedContext;
@property (nonatomic, strong) NSMutableArray * sourceArray;
@end

@implementation GifCollectionViewController

static NSString * const reuseIdentifier = @"CollectionViewCell";
static NSString * const headReuseIdentifier = @"CollectionViewHeader";

- (void)viewDidLoad {
    [super viewDidLoad];
    AppDelegate * appdele = (AppDelegate *)[UIApplication sharedApplication].delegate;
    self.managedContext = appdele.managedObjectContext;
    if (self.isDownload) {
        NSArray * results = [[SaveFile shareFile] getAllDownloadGifs];
        self.sourceArray = [NSMutableArray arrayWithArray:results];
    }else{
        NSArray * results = [[SaveFile shareFile] getAllCollectionGifs];
        self.sourceArray = [NSMutableArray arrayWithArray:results];
    }
    self.collectionView.backgroundColor = [UIColor whiteColor];
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Register cell classes
    [self.collectionView registerClass:[GifCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headReuseIdentifier];
    if (self.sourceArray.count == 0) {
        NSString * alertMessage = nil;
        if(self.isDownload){
            alertMessage = @"您还没有下载任何内容呢^_^";
        }else{
            alertMessage = @"您还没有收藏任何内容呢^_^";
        }
        UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"提示：" message:alertMessage preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction * alertAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            [self.navigationController popViewControllerAnimated:YES];
        }];
        [alertController addAction:alertAction];
        UIImageView * imageview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
        imageview.center = self.view.center;
        imageview.image = [UIImage imageNamed:@"iconfont-xiao1"];
        [self.view addSubview:imageview];
        [self presentViewController:alertController animated:YES completion:nil];
    }
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
#warning Incomplete method implementation -- Return the number of sections
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
#warning Incomplete method implementation -- Return the number of items in the section
    return self.sourceArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    // Configure the cell
    GifCollectionViewCell * gifCell = (GifCollectionViewCell *)cell;
    gifCell.gifItem = self.sourceArray[indexPath.row];
    
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(self.view.frame.size.width*0.3, self.view.frame.size.height*0.2);
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
}

//- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
//    
//}
#pragma mark <UICollectionViewDelegate>
/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/


// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
//- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
//	return YES;
//}
//
//- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
//	return YES;
//}
//
//- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
//	
//}

@end
